from django.shortcuts import render
from django.http import HttpResponse
from storage.models import App, Level, Sound
from django.http import HttpResponseRedirect 
from django.shortcuts import render_to_response
from django.template import RequestContext
import json
import re


def ajax_publish(request):
    name = str(request.GET.get('filename'))
    appname = str(request.GET.get('appname'))
    additive = str(request.GET.get('additive'))

    if name == 'app':
        app = App.objects.filter(name=appname).first()
        if additive != 'false':
            app.version += 1
            app.save()

        data = {}
        data['name'] = app.name
        data['version'] = app.version
        data['force_update_version'] = app.force_update_version
        return HttpResponse(json.dumps(data), content_type='application/json')

    elif name == 'level':
        levelData = []
        app = App.objects.filter(name=appname).first()
        for order in app.level_order.all():
            level = Level.objects.filter(name=order.name).first()
            data = {}
            data['name'] = level.name
            data['sound'] = level.sound.name
            data['ios_md5'] = level.ios_md5
            data['android_md5'] = level.android_md5
            levelData.append(data)

        pack = {}
        pack['version'] = app.version
        pack['data'] = levelData
        return HttpResponse(json.dumps(pack, sort_keys=True), content_type='application/json')


def getVersion(request):
    appname = str(request.GET.get('appname'))
    # apps
    app = App.objects.filter(name=appname).first()
    return app.version


def ajax_upload_sounds(request):
    name = str(request.GET.get('name'))
    url = str(request.GET.get('url'))
    md5 = str(request.GET.get('md5'))

    Sound.objects.create(name=name, sound=url, md5=md5)

    jsonData={}
    return HttpResponse(json.dumps(jsonData, sort_keys=True), content_type='application/json')


def ajax_upload_level(request):
    name = str(request.GET.get('name'))
    image = str(request.GET.get('image'))
    ios = str(request.GET.get('ios'))
    android = str(request.GET.get('android'))

    ios_md5 = str(request.GET.get('ios_md5'))
    android_md5 = str(request.GET.get('android_md5'))

    sound = None
    pos = name.find("_")
    if pos != -1:
        sound = Sound.objects.filter(name=name[0:pos]).first()
    
    Level.objects.create(name=name, image=image, ios=ios, android=android, ios_md5=ios_md5, android_md5=android_md5,
                         sound=sound)

    jsonData={}
    return HttpResponse(json.dumps(jsonData, sort_keys=True), content_type='application/json')


def ajax_batch_check_level(request):
    data = request.GET.getlist('data')

    found = []
    for level_name in data:
        for query in Level.objects.all():
            if level_name == query.name:
                found.append(level_name)

    return HttpResponse(json.dumps(found, sort_keys=True), content_type='application/json')


def ajax_check_levels_sound(request):
    resultData = []

    name = str(request.GET.get('appname'))
    app = App.objects.filter(name__in=[name,]).first()
    for query in Level.objects.all():
        if query.sound == None:
            data = {}
            data['name'] = str(query.name)
            resultData.append(data)
    
    return HttpResponse(json.dumps(resultData, sort_keys=True), content_type='application/json')